﻿using System;
using System.Windows.Forms;
using HRM.HRMServiceProxy;

namespace HRM
{
    public partial class frmCertificateInfo : Form
    {
        CertificateInfo _CertificateInfo;
        int _EmployeeID;
        int _CertificateID;
        bool isChange = false;
        DialogResult dlgResult;
        RoleRightInfo listRole;
        CertificateClient wcfCertificate;
        public frmCertificateInfo(int CertificateID, int EmployeeID, RoleRightInfo _listRole)
        {
            InitializeComponent();
            //Language();
            dlgResult = DialogResult.Cancel;
            listRole = _listRole;
            _CertificateInfo = new CertificateInfo();
            wcfCertificate = Service.Certificate();
            _CertificateInfo.ID = CertificateID;
            _EmployeeID = EmployeeID;
            _CertificateID = CertificateID;
            if (_CertificateID > 0)
            {
                _CertificateInfo = wcfCertificate.Certificate_GetInfo(_CertificateID);
                Info2Form();
                wcfCertificate.Close();
            }

        }

        private void Language()
        {
            if (this.Tag != null)
                this.Text = UpdateUI.GetString(this.Tag.ToString());
            UpdateUI.Load_ToolStrip(ref toolStrip1);
            UpdateUI.Load_Panel(ref panel2);
        }

        void Info2Form()
        {
            txtCertificate_Name.Text = _CertificateInfo.Name;
            txtCertificate_AuthorityIssue.Text = _CertificateInfo.AuthorityIssue;
            if (_CertificateInfo.IssueDate != DateTime.MinValue)
                txtCertificate_IssueDate.Text = _CertificateInfo.IssueDate.ToString("dd/MM/yyyy");
            else
                txtCertificate_IssueDate.Text = "";
            txtCertificate_Grade.Text = _CertificateInfo.Grade;
            txtCertificate_Special.Text = _CertificateInfo.Speciality;
            txtCertificate_Description.Text = _CertificateInfo.Description;
            cboKind.Text = _CertificateInfo.Kind;
            isChange = false;
        }

        void Form2Info()
        {
            _CertificateInfo.ID = _CertificateID;
            _CertificateInfo.EmployeeID = _EmployeeID;
            _CertificateInfo.Name = txtCertificate_Name.Text.Trim();
            _CertificateInfo.AuthorityIssue = txtCertificate_AuthorityIssue.Text.Trim();
            _CertificateInfo.IssueDate = UISetting.GetDate(txtCertificate_IssueDate.Text);
            _CertificateInfo.Grade = txtCertificate_Grade.Text.Trim();
            _CertificateInfo.Speciality = txtCertificate_Special.Text.Trim();
            _CertificateInfo.Description = txtCertificate_Description.Text.Trim();
            _CertificateInfo.Kind = cboKind.Text;
        }

        bool CheckValid()//kiểm tra dữ liệu nhập
        {
            if (txtCertificate_Name.Text.Trim().Length == 0)
            {
                txtCertificate_Name.Focus();
                MessageBox.Show(UpdateUI.GetString("tenbangcapkhongduocdetrong"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return false;
            }

            if (txtCertificate_AuthorityIssue.Text.Trim().Length == 0)
            {
                txtCertificate_AuthorityIssue.Focus();
                MessageBox.Show(UpdateUI.GetString("tennoicapkhongduocdetrong"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return false;
            }

            if (!CheckTextValid.IsDateTime(txtCertificate_IssueDate.Text, "dd/MM/yyyy"))
            {
                txtCertificate_IssueDate.Focus();
                MessageBox.Show(UpdateUI.GetString("ngaycapbangkhonghople"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return false;
            }

            //if (txtCertificate_Grade.Text.Trim().Length == 0)
            //{
            //    txtCertificate_Grade.Focus();
            //    MessageBox.Show(UpdateUI.GetString("capdobangkhongduocdetrong"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
            //    return false;
            //}

            if (txtCertificate_Special.Text.Trim().Length == 0)
            {
                txtCertificate_Special.Focus();
                MessageBox.Show(UpdateUI.GetString("chuyennganhkhongduocdetrong"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return false;
            }
            return true;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (!CheckValid())
            {
                isChange = true;
                return;
            }

            CertificateClient wcfCertificate = Service.Certificate();
            Form2Info();
            if (_CertificateID > 0)//update
            {
                if (!listRole.Update)
                {
                    MessageBox.Show(UpdateUI.GetString("khongcoquyencapnhat"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
                if (wcfCertificate.Certificate_Update(_CertificateInfo))
                {
                    Program.taskbarNotifier.Show(UpdateUI.GetString("thongbao"), UpdateUI.GetString("capnhatthanhcong"), UIParamater.TIME_MOVE_UP, UIParamater.TIME_SHOW, UIParamater.TIME_MOVE_DOWN);
                    isChange = false;
                    dlgResult = DialogResult.OK;
                    wcfCertificate.Close();
                }
                else
                {
                    MessageBox.Show(UpdateUI.GetString("capnhatkhongthanhcong"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    isChange = true;
                }
            }
            else//insert
            {
                if (!listRole.Create)
                {
                    MessageBox.Show(UpdateUI.GetString("khongcoquyenthemmoi"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
                _CertificateInfo.ID = wcfCertificate.Certificate_Insert(_CertificateInfo);
                _CertificateID = _CertificateInfo.ID;
                if (_CertificateID > 0)
                {
                    Program.taskbarNotifier.Show(UpdateUI.GetString("thongbao"), UpdateUI.GetString("themmoithanhcong"), UIParamater.TIME_MOVE_UP, UIParamater.TIME_SHOW, UIParamater.TIME_MOVE_DOWN);
                    isChange = false;
                    dlgResult = DialogResult.OK;
                    wcfCertificate.Close();
                }
                else
                {
                    MessageBox.Show(UpdateUI.GetString("themmoikhongthanhcong"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    isChange = true;
                }
            }

        }

        private void btnNew_Click(object sender, EventArgs e)
        {
            if (!listRole.Create)
            {
                MessageBox.Show(UpdateUI.GetString("khongcoquyenthemmoi"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            _CertificateInfo = new CertificateInfo();
            _CertificateID = 0;
            txtCertificate_Name.Text = "";
            txtCertificate_AuthorityIssue.Text = "";
            txtCertificate_IssueDate.Text = "";
            txtCertificate_Grade.Text = "";
            txtCertificate_Special.Text = "";
            txtCertificate_Description.Text = "";
        }

        private void frmCertificateInfo_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (isChange)
            {
                if (MessageBox.Show(UpdateUI.GetString("bandathaydoidulieu") + "\n" + UpdateUI.GetString("coluutruockhithoatkhong"), UIParamater.TitleProject, MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    btnSave_Click(null, null);
                    if (isChange)
                        e.Cancel = true;
                    else
                        e.Cancel = false;
                }
            }
            this.DialogResult = dlgResult;
        }

        private void frmCertificateInfo_KeyUp(object sender, KeyEventArgs e)
        {
            switch (e.KeyCode)
            {
                case Keys.Enter:
                    if (!e.Alt && !e.Control)
                    {
                        this.ProcessTabKey(!e.Shift);
                        e.Handled = true;
                    }
                    break;
                case Keys.Escape:
                    this.Close();
                    break;
                case Keys.F2:
                    btnNew_Click(null, null);
                    break;
                case Keys.F3:
                    btnSave_Click(null, null);
                    break;
            }
        }

        private void DataChange(object sender, EventArgs e)
        {
            isChange = true;
        }
    }
}
